package wx

import (
	"jlpay-sdk-go/pkg/jlpay/core"
	"jlpay-sdk-go/pkg/jlpay/models"
)

// ==================== 微信授权绑定请求 ====================

// AuthBindRequest 微信授权绑定请求
type AuthBindRequest struct {
	models.OrgBaseRequest
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// PayType 交易类型
	PayType string `json:"pay_type,omitempty"`
	// JsapiPath JSAPI支付授权目录
	JsapiPath string `json:"jsapi_path,omitempty"`
	// SubAppid 公众账号ID
	SubAppid string `json:"sub_appid,omitempty"`
	// MchCreateIP 终端IP
	MchCreateIP string `json:"mch_create_ip,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewAuthBindRequest 创建微信授权绑定请求
func NewAuthBindRequest() *AuthBindRequest {
	return &AuthBindRequest{}
}

// SetMchID 设置商户号
func (r *AuthBindRequest) SetMchID(mchID string) *AuthBindRequest {
	r.MchID = mchID
	return r
}

// SetPayType 设置交易类型
func (r *AuthBindRequest) SetPayType(payType string) *AuthBindRequest {
	r.PayType = payType
	return r
}

// SetJsapiPath 设置JSAPI支付授权目录
func (r *AuthBindRequest) SetJsapiPath(jsapiPath string) *AuthBindRequest {
	r.JsapiPath = jsapiPath
	return r
}

// SetSubAppid 设置公众账号ID
func (r *AuthBindRequest) SetSubAppid(subAppid string) *AuthBindRequest {
	r.SubAppid = subAppid
	return r
}

// SetMchCreateIP 设置终端IP
func (r *AuthBindRequest) SetMchCreateIP(mchCreateIP string) *AuthBindRequest {
	r.MchCreateIP = mchCreateIP
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *AuthBindRequest) SetRawRequestMap(rawMap map[string]interface{}) *AuthBindRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始映射数据
func (r *AuthBindRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, r.rawRequestMap != nil
}

// ToMap 转换为map
func (r *AuthBindRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始映射数据，直接返回
	if rawMap, exists := r.GetRawMap(); exists {
		return rawMap, nil
	}

	// 构建基础请求数据
	requestMap := make(map[string]interface{})

	// 添加基础字段
	if r.MchID != "" {
		requestMap["mch_id"] = r.MchID
	}
	if r.PayType != "" {
		requestMap["pay_type"] = r.PayType
	}
	if r.JsapiPath != "" {
		requestMap["jsapi_path"] = r.JsapiPath
	}
	if r.SubAppid != "" {
		requestMap["sub_appid"] = r.SubAppid
	}
	if r.MchCreateIP != "" {
		requestMap["mch_create_ip"] = r.MchCreateIP
	}

	// 合并基础请求数据
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	for k, v := range baseMap {
		requestMap[k] = v
	}

	return requestMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *AuthBindRequest) CheckRequiredParams(config *core.Config) error {
	return r.OrgBaseRequest.CheckRequiredParams(config)
}

// ==================== 微信授权绑定响应 ====================

// AuthBindResponse 微信授权绑定响应
type AuthBindResponse struct {
	models.StandardResponse
	// 由于没有对应的Response.java文件，响应只包含ret_code和ret_msg
}

// NewAuthBindResponse 创建微信授权绑定响应
func NewAuthBindResponse() *AuthBindResponse {
	return &AuthBindResponse{}
}
